var tabindex=300;
function get_new_scout_report(scout_id)
{
	player_info["scout"] = null;
	get_player_info("scout",scout_id);
}
function show_player_history(stats_type,data) {
	var $table = $("<table/>").addClass("history_table zebra")
		.append("<thead><tr><th tooltip=\""+pagecontent[17]+"\">"+pagecontent[18]+"</th><th>"+pagecontent[19]+"</th><th>"+pagecontent[20]+"</th><th tooltip=\""+pagecontent[28]+"\">"+pagecontent[21]+"</th>"+(player_fp=="gk"?"<th tooltip='"+pagecontent[66]+"'>"+pagecontent[66]+"</th>" :"<th tooltip='"+pagecontent[23]+"'>"+pagecontent[22]+"</th>")+"<th tooltip=\""+pagecontent[25]+"\">"+pagecontent[24]+"</th><!--<th tooltip=\""+pagecontent[27]+"\">"+pagecontent[26]+"</th>--><th>"+pagecontent[29]+"</th><th>"+pagecontent[30]+"</th></tr></thead>");
	$table.attr("cellspacing","0").attr("cellpadding","0");
	var $tbody = $("<tbody/>").appendTo($table);
	var no_pro_rows = 0;
	var season = data["current_season"];
	// Loop seasons
	for(var i in data["table"][stats_type])
	{
		var player = data["table"][stats_type][i];
		if(player)
		{
			var transfer_colspan = player["no_pro"] != null  ? 1 : 7;
			var $player = $("<tr/>");
			if(player["season"] == "total")
			{
				$player
					.append("<td></td>")
					.append("<td>"+pagecontent[85]+"</td>")
					.append("<td></td>")
					.append("<td>"+player["games"]+"</td>")
					.append("<td>"+(player_fp=="gk"?+player["conceded"]:player["goals"])+"</td>")
					.append("<td>"+player["assists"]+"</td>")
					.append("<td>"+player["cards"]+"</td>")
					.append("<td>"+player["rating_avg"]+"</td>").addClass("total large");
			}
			else if(player["season"] == "transfer")
			{
				$player
					.append("<td colspan=\"2\">"+pagecontent[46]+" "+player["transfer"]+"</td>")
					.append("<td colspan=\""+transfer_colspan+"\"></td>").addClass("transfer_row");
					no_pro_rows++;
			}
			else
			{
				$player
					.append("<td>"+player["season"]+"</td>")
					.append("<td class=\" club\"><div class='text_fade'><div class=\"club_name\">"+player["klubnavn"]+"</div></div></td>")
					.append("<td><div class=\"division\">"+player["division_string"]+"</div></td>");
				if(player["no_pro"] && i > 0)
				{ // User has no pro
					if(no_pro_rows == 0)
					{
						$player
							.append("<td colspan=\"8\" id=\"no_pro\" style=\"vertical-align: middle;\"><a href=\"/buy-pro/\" title='"+pagecontent[47]+"'><img src=\"http://static.trophymanager.com/pics/payment_box_1.png\"/></a></td>");
					}
					no_pro_rows++;
				}
				else
				{
					$player
						.append("<td>"+player["games"]+"</td>")
						.append("<td>"+(player_fp=="gk"?+player["conceded"]:player["goals"])+"</td>")
						.append("<td>"+player["assists"]+"</td>")
						.append("<td>"+player["cards"]+"</td>")
						.append("<td>"+player["rating_avg"]+"</td>");
				}
			}
			$player.appendTo($tbody);
			season--;
		}
	}
	$("#player_history_data").html($table);
	table_zebra($table);
	// Make text fade
	$table.find(".text_fade").each(function(){
		if($(this).closest("tr").hasClass("odd"))
		{
			$(this).parent().addClass("odd");
		}
		make_text_fade($(this));
	});
	$("#no_pro").attr("rowspan",no_pro_rows);
	// Make zebra stripes
}
var player_history_data = {};
function get_player_info(type,scout_id)
{
	if(player_info[type] == null && player_id > 0)
	{
		if(type != "training") $("#player_"+type+"_new").html(get_loading_img("small"));
		$.post("/ajax/players_get_info.ajax.php",{"player_id":player_id, "type":type,"scout_id":scout_id,"show_non_pro_graphs":show_non_pro_graphs},function(data){
			if(data != null)
			{
				if(type == "history")
				{ // Player History
					player_history_data = data;
					var $select = $("<select />").attr("id","player_history_select").change(function(){
						show_player_history($(this).val(),player_history_data);
					});
					var stats_names = {"nat":pagecontent[112],"int":pagecontent[113],"cup":pagecontent[114],"total":pagecontent[115]};
					for(var i in data.table) {
						if(data.table[i].length) {
							var $o = $("<option />").val(i).append(stats_names[i]).appendTo($select);
							if(i == "nat") $o.attr("selected",true);

						}
					}
					$("#player_history_new").html("").append(
						$select,
						$("<div />").attr("id","player_history_data")
						);
					show_player_history("nat",data);
					$select.selectmenu();
					/*
					var $table = $("<table/>").addClass("history_table zebra")
							.append("<thead><tr><th tooltip=\""+pagecontent[17]+"\">"+pagecontent[18]+"</th><th>"+pagecontent[19]+"</th><th>"+pagecontent[20]+"</th><th tooltip=\""+pagecontent[28]+"\">"+pagecontent[21]+"</th>"+(player_fp=="gk"?"<th tooltip='"+pagecontent[66]+"'>"+pagecontent[66]+"</th>" :"<th tooltip='"+pagecontent[23]+"'>"+pagecontent[22]+"</th>")+"<th tooltip=\""+pagecontent[25]+"\">"+pagecontent[24]+"</th><!--<th tooltip=\""+pagecontent[27]+"\">"+pagecontent[26]+"</th>--><th>"+pagecontent[29]+"</th><th>"+pagecontent[30]+"</th></tr></thead>");
					$table.attr("cellspacing","0").attr("cellpadding","0");
					var $tbody = $("<tbody/>").appendTo($table);
					var no_pro_rows = 0;
					var season = data["current_season"];
					// Loop seasons
					for(var i in data["table"])
					{
						var player = data["table"][i];
						if(player)
						{
							var transfer_colspan = player["no_pro"] != null  ? 1 : 7;
							var $player = $("<tr/>");
							if(player["season"] == "total")
							{
								$player
									.append("<td></td>")
									.append("<td>"+pagecontent[85]+"</td>")
									.append("<td></td>")
									.append("<td>"+player["games"]+"</td>")
									.append("<td>"+(player_fp=="gk"?+player["conceded"]:player["goals"])+"</td>")
									.append("<td>"+player["assists"]+"</td>")
									.append("<td>"+player["cards"]+"</td>")
									.append("<td>"+player["rating_avg"]+"</td>").addClass("total large");
							}
							else if(player["season"] == "transfer")
							{
								$player
									.append("<td colspan=\"2\">"+pagecontent[46]+" "+player["transfer"]+"</td>")
									.append("<td colspan=\""+transfer_colspan+"\"></td>").addClass("transfer_row");
									no_pro_rows++;
							}
							else
							{
								$player
									.append("<td>"+player["season"]+"</td>")
									.append("<td class=\" club\"><div class='text_fade'><div class=\"club_name\">"+player["klubnavn"]+"</div></div></td>")
									.append("<td><div class=\"division\">"+player["division_string"]+"</div></td>");
								if(player["no_pro"] && i > 0)
								{ // User has no pro
									if(no_pro_rows == 0)
									{
										$player
											.append("<td colspan=\"8\" id=\"no_pro\" style=\"vertical-align: middle;\"><a href=\"/buy-pro/\" title='"+pagecontent[47]+"'><img src=\"http://static.trophymanager.com/pics/payment_box_1.png\"/></a></td>");
									}
									no_pro_rows++;
								}
								else
								{
									$player
										.append("<td>"+player["games"]+"</td>")
										.append("<td>"+(player_fp=="gk"?+player["conceded"]:player["goals"])+"</td>")
										.append("<td>"+player["assists"]+"</td>")
										.append("<td>"+player["cards"]+"</td>")
										.append("<td>"+player["rating_avg"]+"</td>");
								}
							}
							$player.appendTo($tbody);
							season--;
						}
					}
					$("#player_history_new").html($table);
					table_zebra($table);
					// Make text fade
					$table.find(".text_fade").each(function(){
						if($(this).closest("tr").hasClass("odd"))
						{
							$(this).parent().addClass("odd");
						}
						make_text_fade($(this));
					});
					$("#no_pro").attr("rowspan",no_pro_rows);
					// Make zebra stripes
					*/
				}
				else if(type == "scout" || type > 0)
				{ // Player Scout Reports
					if (data["scouts"].length<1)
					{
						var $div = $("<div style=\"padding: 3px;\"/>").addClass((i%2==0?"odd":"")).appendTo("#player_scout_new");
						//var $table = $("<table />").addClass("zebra");
						//var $tbody = $("<tbody />").append("<tr><td><a href='/scouts/'>"+data['scout_content'][74]+"</a></td></tr>").appendTo($table);
						var $span = $("<span><a href='/scouts/hire/'>"+data['scout_content'][78]+"</a></span>").addClass("subtle");
						$("#player_scout_new").html($span);
					}
					if (data["reports"].length>-1)
					{
						var $table = $("<table />").addClass("zebra");
						var $tbody = $("<tbody />").append("<tr>"
							+"<th>"+global_content["name"]+"</th>"
							+"<th tooltip=\""+global_content["seniors"]+"\">"+global_content["sen"]+"</th>"
							+"<th tooltip=\""+global_content["youths"]+"\">"+global_content["yth"]+"</th>"
							+"<th tooltip=\""+global_content["physical"]+"\">"+global_content["phy"]+"</th>"
							+"<th tooltip=\""+global_content["tactical"]+"\">"+global_content[482]+"</th>"
							+"<th tooltip=\""+global_content["technical"]+"\">"+global_content[483]+"</th>"
							+"<th tooltip=\""+global_content["development"]+"\">"+global_content["dev"]+"</th>"
							+"<th tooltip=\""+global_content["psychology"]+"\">"+global_content["psy"]+"</th>"
							+"<th></th></tr>").appendTo($table);
						if (scout_id > 0)
						{
							//NEW REPORT type = scoutid
						}
						if (data["error"])
						{
							var report_error = data["error"];
						}

						for(var i in data["scouts"])
						{
							var scout = data["scouts"][i];
							if(scout)
							{
								var button = "<span class=\"button disabled\" tooltip='"+scout["returns"]+"'><span class=\"button_border\">"+pagecontent[48]+"</span></span>";
								if (!scout["away"])
								{
									button = "<span onclick=\"javascript:get_new_scout_report("+scout["id"]+")\" class=\"button\"><span class=\"button_border\">"+pagecontent[48]+"</span></span>";
								}
								var $tr = $("<tr/>").appendTo($tbody);
									$tr
									.append("<td>"+scout["name"]+" "+scout["surname"]+"</td>")
									.append("<td>"+scout["seniors"]+"</td>")
									.append("<td>"+scout["youths"]+"</td>")
									.append("<td>"+scout["physical"]+"</td>")
									.append("<td>"+scout["tactical"]+"</td>")
									.append("<td>"+scout["technical"]+"</td>")
									.append("<td>"+scout["development"]+"</td>")
									.append("<td>"+scout["psychology"]+"</td>")
									.append("<td>"+button+"</td>");
							}
						}
						$("#player_scout_new").html($table);
						table_zebra($table);
						for(var i in data["reports"])
						{
							var report = data["reports"][i];
							if(report && !report_error)
							{

							//////////////////////////// BLOOM INFO ////////////////////////////////
							var bloom_status = "";
							var dev_status = "";
							var report_age = parseInt(report["report_age"]);
							var bloom_age = parseInt(report["bloom_age"]);
							var rem_pot = parseFloat(report["remaining_pot"]);
							var skill_names = ["",global_content["strength"],global_content["stamina"],global_content["pace"],global_content["marking"],global_content["tackling"],global_content["workrate"],global_content["positioning"],global_content["passing"],global_content["crossing"],global_content["technique"],global_content["heading"],global_content["finishing"],global_content["longshots"],global_content["set_pieces"]];
							var skill_names_gk = ["",global_content["strength"],global_content["stamina"],global_content["pace"],global_content["handling"],global_content["one_on_ones"],global_content["reflexes"],global_content["aerial_ability"],global_content["jumping"],global_content["communication"],global_content["kicking"],global_content["throwing"],data['scout_content'][80]];
							if (report["bloom_status_txt"]!="") {
								bloom_status = report["bloom_status_txt"];
								dev_status = report["dev_status"];
							}
	/*
	(+) unique (20) potential
	(+) is in the middle of his bloom
	(+) is in the middle of his development
	( ) good physique (3/4)
	( ) good understanding of the tactical aspects
	( ) Technically your scout estimates H. Jakobsen to have lots of potential for improvement (4/4)
	( ) may naturally develop good strength
	( ) demonstrates OK (3/5) leadership ability
	( ) normal (2/5) professionalism
	( ) a not particularly aggressive (2/5) type
	*/
								if (report["scout_name"]=="YD") report["scout_name"] = data['scout_content'][1];
								var $div = $("<div style=\"padding: 3px;\"/>").addClass((i%2==0?"odd":"")).appendTo("#player_scout_new");
								$div.append("<div class=\"report_header\"><strong>"+report["scout_name"]+"</strong> <span class=\"float_right\">("+report["done"]+")</span></div>");
								if (report["rec"]>0) {
									$div.append(
										$("<div>").append(
											show_youth_rec(report["rec"],(report["potential"]/2))
										).tooltip("<div style='line-height: 40px;'><span class='megastar recomendation' style='vertical-align:middle'></span> "+pagecontent["y30"]+"</div><div style='line-height: 40px;'><span class='megastar potential' style='vertical-align:middle'></span> "+pagecontent["y31"]+"</div>"));
								} else {
									$div.append("<div>"+show_youth_rec((report["current_skill"]/2),(report["potential"]/2))+"</div>");
								}
								$div.append("<div><span class='subtle'>"+global_content['age']+":</span> "+report["report_age"]+"</div>");
								if (report["old_pot"]>0) $div.append("<div><span class='subtle' style='text-transform:capitalize'>"+report['old_pot_txt']+":</span> "+pot_desc[report["old_pot"]]+" ("+report["old_pot"]+") </div>");
								if (report["scoutid"]>0)
								{
									$div.append("<div><span class='subtle'>"+data['scout_content'][37]+":</span> "+bloom_status+"</div>");
									$div.append("<div><span class='subtle'>"+data['scout_content'][45]+":</span> "+dev_status+"</div>");
									//$div.append("<div>inf: "+report['peak_phy']+", "+report['peak_tac']+", "+report['peak_tec']+", "+report['charisma']+", "+report['professionalism']+", "+report['aggression']+"</div>");
								}
								if (report["specialist"]>0)
								{
									if(player_fp=="gk")
									{
										if (report["specialist"]>12) report["specialist"] = 12;
										$div.append("<div><span class='subtle'>"+data['scout_content'][67]+":</span> "+skill_names_gk[report["specialist"]]+"</div>");
									}
									else
									{
										$div.append("<div><span class='subtle'>"+data['scout_content'][67]+":</span> "+skill_names[report["specialist"]]+"</div>");
									}
								}
								if (report['peak_phy_txt']!="" || report['peak_tac_txt']!="" || report['peak_tec_txt']!="" || report['charisma']>0 || report['professionalism']>0 || report['aggression']>0)
								{
									$div.append("<div><span class='subtle'>"+pagecontent[86]+"</span>");
									if (report['peak_phy_txt']!="") $div.append(report['peak_phy_txt'],"<br />");
									if (report['peak_tac_txt']!="") $div.append(report['peak_tac_txt'],"<br />");
									if (report['peak_tec_txt']!="") $div.append(report['peak_tec_txt'],"<br />");
									if (report['charisma']>0)
									{
										if (report['charisma']>18) cha_eval = data['scout_content'][27];
										else if (report['charisma']>14) cha_eval = data['scout_content'][28];
										else if (report['charisma']>12) cha_eval = data['scout_content'][30];
										else if (report['charisma']>5) cha_eval = data['scout_content'][31];
										else if (report['charisma']>0) cha_eval = data['scout_content'][33];
										$div.append(" - "+cha_eval+" "+data['scout_content'][35]+"<br />");
									}
									if (report['professionalism']>0)
									{
										if (report['professionalism']>18) pro_eval = data['scout_content'][27];
										else if (report['professionalism']>14) pro_eval = data['scout_content'][28];
										else if (report['professionalism']>12) pro_eval = data['scout_content'][30];
										else if (report['professionalism']>5) pro_eval = data['scout_content'][31];
										else if (report['professionalism']>0) pro_eval = data['scout_content'][33];
										$div.append(" - "+pro_eval+" "+data['scout_content'][34]+"<br />");
									}
									if (report['aggression']>0)
									{
										if (report['aggression']>18) agg_eval = data['scout_content'][61];
										else if (report['aggression']>14) agg_eval = data['scout_content'][62];
										else if (report['aggression']>12) agg_eval = data['scout_content'][63];
										else if (report['aggression']>5) agg_eval = data['scout_content'][64];
										else if (report['aggression']>0) agg_eval = data['scout_content'][65];
										$div.append(" - "+agg_eval+"<br />");
									}
									$div.append("</div>");
								}
							}
						}
					}
					if (data["error"])
					{
						if (report_error=="multi_bid")
						{
							report_error = pagecontent[67];
						}
						if (report_error=="multi_scout")
						{
							report_error = pagecontent[67];
						}
						var $div = $("<div style=\"padding: 3px; font-weight: 900\"/>").appendTo("#player_scout_new");
						$div.append(report_error);

					}
					if(data["interested"])
					{
						var inter = data["interested"];
						var $table = $("<table/>").attr("cellspacing","0").attr("cellpadding","0").addClass("zebra interested_clubs");
						var $tbody = $("<tbody />").appendTo($table);
						for(var i in inter)
						{
							club = inter[i];
							if(club)
							{
								var $tr = $("<tr />")
									.append("<td class=\"align_center\">"+club["message_link"]+"</td>")
									.append("<td>"+club["club_link"]+" "+online_indicator(club["online"])+"</td>")
									.append("<td>"+club["league_link"]+"</td>")
									.append("<td>"+club["cash"]+"</td>")
									.appendTo($tbody);
							}
						}
						if(inter.length == 0)
						{
							$table = $("<div>"+pagecontent[49]+"</div>");
						}
						$("#player_scout_new").append($("<h3/>").html(pagecontent[108]),$table);
						table_zebra($table);
						activate_club_links($table.find("[club_link]"));
						activate_league_links($table.find("[league_link]"));
					}
					/*for(var i in skill_names)
					{
						$div.append("<br />"+i+": "+skill_names[i]+"");
					}
					for(var i in data['scout_content'])
					{
						$div.append("<div>"+i+": "+data['scout_content'][i]+"</div>");
					}
					for(var i in global_content)
					{
						$div.append("<div>"+i+": "+global_content[i]+"</div>");
					}*/
				}
				else if(type == "graphs")
				{ // Player training
					$("#player_graphs_new").html("");
					if(data.graphs.ti)
					{
						var ti_level = "";
						var ti = parseInt(data.graphs.ti[data.graphs.ti.length-1]);
						if(ti<1) ti_level = global_content[539];
						else if(ti < 5) ti_level = global_content[540];
						else if(ti < 10) ti_level = global_content[541];
						else if(ti < 15) ti_level = global_content[542];
						else ti_level = global_content[543];
						$("#player_graphs_new").append(
							$("<p/>").addClass("std").html(global_content[536]+" "+ti_level).tooltip(global_content[534]+" "+ti)
						);
						if(data.skillpoints.up.length)
						{
							var skills = [];
							for(var i in data.skillpoints.up)
							{
								skills.push(global_content[data.skillpoints.up[i]]);
							}
							$("#player_graphs_new").append(
								$("<p/>").addClass("std").html(global_content[537]+" "+skills.join(", "))
							);
						}
					}
					if(is_pro || show_non_pro_graphs)
					{
						var player_data = data.player;
						data=data.graphs;
						$("#player_graphs_new").append($("<div />").attr("id","graph"));

	//						var plot = players_by_id[player_id]["plot"];
						var graphs = ["ti","skills","peaks","skill_index","recommendation"];
						var enabled_graphs = 0;
						for(var i in graphs)
						{
							var graph = graphs[i];
							var plot = data[graph];
							var graph_enabled = !(!plot && (graph!="skills" || !data["strength"]));
							// Only visible if people have read the announcement
//							if(!new_graphs && graph!="ti" && !graph_enabled) continue;
							// add header
							if(show_non_pro_graphs && graph=="ti" && !is_pro) continue;
							if(graph_enabled || is_users_player)
							{
								var h = graph_headers[graph] ? graph_headers[graph] : global_content[graph];
								var $desc = graph_desc[graph]?$("<p />").addClass("subtle std").html(graph_desc[graph]):[];
								$("#player_graphs_new").removeClass("std").append(
									$("<div />").addClass("graph_"+graph+" background_gradient graph_block").attr("style","padding-bottom:10px").append(
									$("<div class='graph_header' style='background: url(http://static.trophymanager.com/pics/content_menu_stats.png) no-repeat 5px center;padding:10px 0 10px 35px;'>").html(h+' <img src="http://static.trophymanager.com/pics/pro_icon.png" class="pro_icon" style="position:relative;top:2px;"/>'),
									$desc,
									$("<div />").addClass("graph").attr("id","graph_"+graph)
									)
								);

								if(!graph_enabled)
								{
									$(".graph_"+graph).find(".graph_header").append(make_button("Enable "+h,"graph_enable("+player_id+",'"+graph+"')",'float_right'));
									continue;
								}
								else if(graph!="ti") enabled_graphs++;
							}
							else continue;
							var skill_colors = {
								"strength":"#ffff00",
								"stamina":"#00ffff",
								"pace":"#ff00ff",
								// !gk
								"marking":"#ff0000",
								"tackling":"#00dd00",
								"workrate":"#0000ff",
								"positioning":"#ff8800",
								"passing":"#6600aa",
								"crossing":"#0055ff",
								"technique":"#aa0066",
								"heading":"#222222",
								"finishing":"#99ff00",
								"longshots":"#00cc66",
								"set_pieces":"#ffffff",
								// gk
								"handling":"#ff0000",
								"one_on_ones":"#00dd00",
								"reflexes":"#0000ff",
								"aerial_ability":"#ff8800",
								"jumping":"#6600aa",
								"communication":"#0055ff",
								"kicking":"#aa0066",
								"throwing":"#222222",
								// peaks
								"physical":"#ffff00",
								"tactical":"#00ffff",
								"technical":"#ff00ff"
							};
							if(graph=="skills")
							{
								var target = 'graph_'+graph;
								var $form = $("<form />").attr("id",target+"_form").addClass("std").appendTo(".graph_"+graph).attr("style","margin:10px 0").append($("<div />").addClass("skill_block").attr("style","width: 210px;float:left;margin-left:20px;"));
								if(data.strength.length>0)
								{
									for(var j in data)
									{
										if(j!="ti"&&j!="skill_index"&&j!="recommendation" && j!="peaks")
										{
											if(j=="handling" || j=="passing")
											{
												$form.append($("<div />").addClass("skill_block").attr("style","width: 210px;float:left;margin-left:20px;"));
											}
											var plot = data[j];
											var plot2 = [];
											var start_months = player_data.years*12+player_data.months-plot.length+1;
											var months=start_months;
											var start_years = Math.floor(start_months/12);
											var xinterval = parseInt(plot.length/5);
											var xmax = plot.length+5-(plot.length%5);//player_data.years+1;
											var xmin = 0;//start_months%12>0?start_years:start_years-1;
											for(var i in plot)
											{
												plot2.push(plot[i]);//[(months/12),Number(plot[i]),plot[i]]);
												months++;
											}
											skill_plots[j] = plot2;
//											skill_plots[j] = data[j];
											$form.find(".skill_block:last").append(
												$("<input />").attr("type","checkbox").attr("target",target).attr("id","skill_"+j).attr("skill",j).attr("skill_color",skill_colors[j]).attr("xmin",xmin).attr("xmax",xmax).attr("xinterval",xinterval).change(function(){
													make_skill_plots($(this).attr("target"),null,0,20,2,"%.0f",Number($(this).attr("xmin")),$(this).attr("xmax"),$(this).attr("xinterval"));
												}).attr("checked",false),
												$("<label />").attr("for","skill_"+j).append(
													$("<span />").attr("style","display:inline-block; width: 8px;margin:0px 2px 2px 0;height:8px;background:"+skill_colors[j]+";border-radius:6px;vertical-align:middle;"),
													global_content[j]
												),
												"<br />"
											)
										}
										if(j=="strength") $("#skill_strength").attr("checked",true);
										make_checkbox("skill_"+j);
									}
									$form.append(
										$("<div />").addClass("clear"),
										$("<div />").addClass("").attr("style","text-align:center; padding: 10px 0 0 0").append(
											$("<span />").addClass("button").attr("target",target).append(
												$("<span />").addClass("button_border").html("All")
											).attr("xmin",xmin).attr("xmax",xmax).click(function(){
												$("#"+$(this).attr("target")+"_form").find("input").each(function(){
														$(this).attr("checked",true);
														$("#"+$(this).attr("id")+"_checkbox").addClass("selected");
												});
												make_skill_plots($(this).attr("target"),null,0,20,2,"%.0f",Number($(this).attr("xmin")),Number($(this).attr("xmax")));
											}),
											$("<span />").addClass("button").attr("target",target).append(
												$("<span />").addClass("button_border").html("None")
											).attr("xmin",xmin).attr("xmax",xmax).click(function(){
												$("#"+$(this).attr("target")+"_form").find("input").each(function(){
														$(this).attr("checked",false);
														$("#"+$(this).attr("id")+"_checkbox").removeClass("selected");
												})
												make_skill_plots($(this).attr("target"),null,0,20,2,"%.0f",Number($(this).attr("xmin")),Number($(this).attr("xmax")));
											})
										)
										);
								}
								make_skill_plots(target,null,0,20,2,"%.0f",xmin,xmax,xinterval);
							}
							else if(graph=="peaks")
							{
								var target = 'graph_'+graph;
								var $form = $("<form />").attr("id",target+"_form").addClass("std").appendTo(".graph_"+graph).attr("style","margin:10px 0 0 0");
								if(data.peaks.physical.length>0)
								{
									var ymin=10000,ymax=0;
									for(var j in data["peaks"])
									{
										var plot = data.peaks[j];
										var plot2 = [];
										var start_months = player_data.years*12+player_data.months-plot.length+1;
										var months=start_months;
										var start_years = Math.floor(start_months/12);
										var xmax = player_data.years+1;
										var xmin = start_months%12>0?start_years:start_years-1;
										for(var i in plot)
										{
											plot2.push([(months/12),Number(plot[i]),plot[i]]);
											months++;
											ymax = Math.max(ymax,Number(plot[i]));
											ymin = Math.min(ymin,Number(plot[i]));
										}
										skill_plots[j] = plot2;
										$form.append(
											$("<div />").attr("style","display: inline-block;width:32%;text-align:center;").append(
											$("<input />").attr("type","checkbox").attr("target",target).attr("id","skill_"+j).attr("skill",j).attr("skill_color",skill_colors[j]).attr("xmin",xmin).attr("xmax",xmax).change(function(){
												make_skill_plots($(this).attr("target"),null,null,null,null,"%.0f",Number($(this).attr("xmin")),Number($(this).attr("xmax")));
											}).attr("checked",true),
											$("<label />").attr("for","skill_"+j).append(
												$("<span />").attr("style","display:inline-block; width: 8px;margin:0px 2px 2px 0;height:8px;background:"+skill_colors[j]+";border-radius:6px;vertical-align:middle;"),
												global_content[j]
											)
											)
										)
										make_checkbox("skill_"+j);
									}
									if(ymax==0)ymax=100;
									if(ymin==10000) ymin=0;
								}

								make_skill_plots(target,null,ymin-parseInt(ymin/30),ymax+parseInt(ymax/30),null,"%.0f",xmin,xmax);
							}
							else
							{
								var plot2 = [];
								var max=0,min=100000000000;
								var start_months = player_data.years*12+player_data.months-plot.length+1;
								var months=start_months;
								var start_years = Math.floor(start_months/12);
								var xmax = player_data.years+1;
								var xmin = start_months%12>0?start_years:start_years-1;
								for(var i in plot)
								{
									plot2.push([(months/12),Number(plot[i]),plot[i]]);
									if(Number(plot[i])>max) max = Number(plot[i]);
									if(Number(plot[i])<min) min = Number(plot[i]);
									months++;
								}
								var target = 'graph_'+graph;
								var ymin,ymax,yinterval,ystyle;
								if(graph=="recommendation"){ ymin=0,ymax=5,yinterval=0.5,ystyle='%.1f'}
								else if(graph=="ti"){
									ystyle='%.0f',yinterval=Math.round((max-min)/5);
									if(max-min < 5)
									{
										ymax=max+3, ymin=min;
									}
									else ymax=max+1;
								}
								else if(graph=="skill_index"){ ystyle='%.0f',ymin=min-parseInt(min/20),ymax=max+parseInt(max/20),yinterval=Math.round((ymax-ymin)/5)}
								if(yinterval<0.5) yinterval=1;
								make_plots(target,[plot2],null,ymin,ymax,yinterval,ystyle,xmin,xmax);
							}
						}
						if(is_users_player && !enabled_graphs)
						{ //enable all graphs button
							$("#player_graphs_new").append(
								$("<div />").addClass("align_center").append(
								make_button("Enable all graphs","graph_enable("+player_id+",'all')")
								)
							);
						}
					} // is_pro
					else
					{
						$("#player_graphs_new").append(
							$("<p />").addClass("std").addClass("bold").html(global_content[538]).click(function(){
								window.location="/buy-pro/";
							}),
							$("<div />").addClass("align_center").append(
								$("<a />").attr("href","/buy-pro/").html(
									$("<img />").attr("src","http://static.trophymanager.com/pics/graph_player_profile.png").css("border","1px solid #fff")
								)
							)
						);
					}
//					return false;
				}
				else if(type == "training")
				{ // Player training
					// Custom training
					var $t = $("<div />").addClass("custom_training").attr("id","custom_radio").appendTo("#player_training_new");
					var label1 = global_content["training"]+": "+training_team_names[data["custom"]["team"]];
					if(!data["custom"]["gk"]) label1 += " <a href='/training/'>"+global_content["change"]+"</a>";
					var label2 = global_content["training"]+": "+training_team_names["custom"]+" <a href='/buy-pro/'><img src='http://static.trophymanager.com/pics/pro_icon.png' /></a>";
					if(SESSION["mobile"])
					{
						var $div = $("<div />").append(
							$("<label />").attr("for","training_custom_off").addClass("training_radio_label large").html(label1),
							$("<input />").attr("type","radio").attr("id","training_custom_off").attr("name","training_radio").change(function(){
								if($(this).is(":checked")) select_custom_training()
							})

						).appendTo($t);
						var $div = $("<div />").addClass("odd").append(
							$("<label />").attr("for","training_custom_on").addClass("training_radio_label large").html(label2),
							$("<input />").attr("type","radio").attr("id","training_custom_on").attr("name","training_radio").change(function(){
								if($(this).is(":checked")) select_custom_training(true)
							})
						).appendTo($t);
					}
					else
					{
						var $ul = $("<ul >").addClass("radio_buttons").appendTo($t);
						$ul.append(
								$("<li />").addClass("radio less_space").attr("event","select_custom_training()").attr("id","training_custom_off").attr("radio_val","off").append(
								$("<div />").addClass("radio_label training_radio_label large").html(label1),
								$("<div />").addClass("radio_img")
								)
								);
						if(!data["custom"]["gk"])
						{
							$ul.append(
								$("<li />").addClass("radio odd less_space").attr("event","select_custom_training(true)").attr("id","training_custom_on").attr("radio_val","on").append(
								$("<div />").addClass("radio_label training_radio_label large").html(label2),
								$("<div />").addClass("radio_img")
								)
							);
						}
					}
					custom_training = data["custom"]["custom"];
					if(data["custom"]["custom_on"] && is_pro)
					{
						template_training_custom(true);
						if(SESSION["mobile"]) $t.find("#training_custom_on").attr("checked",true);
						else $t.find("#training_custom_on").addClass("selected");
					}
					else
					{
						if(SESSION["mobile"]) $t.find("#training_custom_off").attr("checked",true);
						else $t.find("#training_custom_off").addClass("selected");
					}
					if(!is_pro) $t.find("#training_custom_on").addClass("disabled");
					if(!SESSION["mobile"]) make_radio("custom_radio");
				}
				else if(type == "interested")
				{ // Interested Clubs
					var $table = $("<table/>").attr("cellspacing","0").attr("cellpadding","0").addClass("zebra interested_clubs");
					var $tbody = $("<tbody />").appendTo($table);
					for(var i in data)
					{
						club = data[i];
						if(club)
						{
							var $tr = $("<tr />")
								.append("<td class=\"align_center\">"+club["message_link"]+"</td>")
								.append("<td>"+club["club_link"]+" "+online_indicator(club["online"])+"</td>")
								.append("<td>"+club["league_link"]+"</td>")
								.append("<td>"+club["cash"]+"</td>")
								.appendTo($tbody);
						}
					}
					if(data.length == 0)
					{
						$table = $("<div>"+pagecontent[49]+"</div>");
					}
					$("#player_interested_new").html($table);
					table_zebra($table);
					activate_club_links($table.find("[club_link]"));
					activate_league_links($table.find("[league_link]"));
				}
				player_info[type] = true;
				if ($table) init_tooltip_by_elems($table.find("[tooltip]"));

			}
		},"json").error(function(){ });
	}
}
function make_skill_plots(target,data,ymin,ymax,yinterval,ystyle,xmin,xmax,xinterval)
{
	$("#"+target).html("").attr("style","height:auto");
	data = data || skill_plots;
	var plots = [];
	var colors = [];
	$("#"+target+"_form").find("input").each(function(){
		if($(this).is(":checked"))
		{
			plots.push(data[$(this).attr("skill")]);
			colors.push($(this).attr("skill_color"));
		}
	});
	if(!plots.length) plots[0] = [];
	make_plots(target,plots,colors,ymin,ymax,yinterval,ystyle,xmin,xmax,xinterval);
}
function make_plots(target,plots,colors,ymin,ymax,yinterval,ystyle,xmin,xmax,xinterval)
{
	$("#"+target).after($("<div />").addClass("align_center loading").html(get_loading_img()));
	setTimeout(function(){
	ystyle=ystyle||'%.0f';
	colors = colors || ["#ffffff"];
	if(plots[0].length==0)
	{
		$("#"+target).html("No data available yet");
		return;
	}
	if(SESSION.id==10) console.log(plots);
	xmin = xmin || 0;
	xmax = xmax || plots[0].length+1;
//	var xinterval = plots[0].length<6 ? 1 : Math.floor(plots[0].length/6);
	if(!xinterval) {
		xinterval = 1;
		if(plots[0].length>12*15) xinterval=2; // more than 15 years of plots
	}
	var plot1 = $.jqplot (target, plots,{
			grid:{
				background:"transparent",
				borderColor:"#6c9922",
				gridLineColor:"#6c9922",
				shadow:false
			},
			axes: {
				xaxis: {
					tickOptions:{formatString:'%.0f'},
					pad: 0,
					min: xmin,
					max: xmax, //plots[0].length+1,
					label: global_content.age+"",
					tickInterval:xinterval
				},
				yaxis: {
					tickOptions:{formatString:ystyle},
					pad: 0,
					min:ymin,
					max:ymax,
					tickInterval:yinterval
				},
			},
			series:[{
//				lineWidth:2,
//				markerOptions: {size:2,style:"circle"}
			}],
			seriesDefaults:{
				showMarker:true,
				lineWidth:3,
				markerOptions: {size:4,style:"circle"},
				pointLabels:{ show:true,"location": "n",edgeTolerance:-20}
			},
			seriesColors:colors
			});
	// add Labels
	$("#"+target+" .jqplot-point-label").hide();
	previousPoint = null;
	$('#'+target).bind('jqplotDataMouseOver', function (ev, seriesIndex, pointIndex, data) {
		var labels = $(this).find('.jqplot-point-label');
		labels.hide();
//						var point = labels.length-pointIndex;
		$(this).find(".jqplot-point-"+pointIndex).show();
	});
	$("#"+target).parent().find(".loading").remove();
	},200);
}
function graph_enable(player_id,graph,confirm,double_confirm)
{
	var h = graph_headers[graph] ? graph_headers[graph] : global_content[graph];
	if(!confirm)
	{
		modal.html(get_loading_img()).show();
		$.post("/ajax/players_hidden_skills.ajax.php",{"type":"get_graph_enable","player_id":player_id},function(data){
			var player_name = data.name;
			var pc0 = graph=="all"?"Enable all graphs for "+player_name:"Enable "+h+" graph for "+player_name;
			var pc1 = graph=="all"?"When you enable these graphs, they will be available for "+player_name+" until he retires.":"When you enable this graph, it will be available for "+player_name+" until he retires.";
			var $d = $("<div />").append(
				$("<h2 />").html(pc0),
				$("<p/>").html(pc1+" New data will be added every week.")
				);
			if(!SESSION["is_diamond"])
			{
				var pro_days = graph=="all"? 4 : 1;
				var pc2 = graph=="all"?"Are you sure you wish to enable all graphs?":"Are you sure you wish to enable this graph?";
				$d.append(
					$("<p/>").html("It will cost you "+pro_days+" pro day(s)."),
					$("<p/>").html(pc2)
					);
			}
			$d.append(
				$("<div />").addClass("msgbuttons").append(
					make_button("Enable","graph_enable("+player_id+",'"+graph+"',true,"+SESSION["is_diamond"]+")"),
					make_button(global_content.cancel,"modal.hide()")
					)
				);
			modal.html($d).show();
		},"json").error(modal_ajax_error_function);
	}
	else
	{
		modal.inner().find(".msgbuttons").html(get_loading_img());
		$.post("/ajax/players_hidden_skills.ajax.php",{"type":"post_graph_enable","graph":graph,"player_id":player_id,"confirm":(double_confirm?1:0)},function(data){
			if(data.success)
			{
				// refresh or something
				window.location.reload(true);
				body_refresh();
			}
			else if(data.error == "few_rows")
			{
				modal.inner().find(".msgbuttons").html("").append(
					$("<p/>").addClass("large bold").html("Warning! This player has less than 10 graph entries, are you sure you wish to enable it?"),
					make_button("Enable","graph_enable("+player_id+",'"+graph+"',true,true)"),
					make_button(global_content.cancel,"modal.hide()")
					);
			}
			else modal_ajax_error_function(data);
		},"json").error(modal_ajax_error_function);
	}
}
var modal_ajax_error_function = function(data){
	console.log(arguments);
};
function select_custom_training(on)
{
	if(on)
	{
		template_training_custom();
	}
	else
	{
		$(".training_custom_levels").html("");
		post_training_custom(0);
	}
}
function post_training_custom(val)
{
	var ar = val==1?custom_training:null;
	$("#player_training_new .last_save").html("Saving "+get_loading_img());
	$.post("/ajax/training_post.ajax.php",{"type":"custom","on":val,"player_id":custom_training["player_id"],"custom":ar},function(data){
		setTimeout(function(){
		$("#player_training_new .last_save").html("");
		if(data["success"])
		{
			$("#player_training_new .last_save").html(global_content[532]+": "+data["time"]+"");
			custom_training["saved"] = global_content[531]+": "+data["time"];
		}
		else
		{
			$("#player_training_new .last_save").html("<span class='error italic'>"+global_content["error"]+": "+data["error"]+"</span>");
		}
		},300);
	},"json").error(function(data){
		setTimeout(function(){
			$("#player_training_new .last_save").html("");$("#player_training_new .last_save").html("<span class='error italic'>"+global_content["error"]+": "+data["error"]+"</span>");
		},300);
		});
}
var custom_training;
function template_training_custom(init)
{
	var	custom = custom_training;
	var $d = $("<div />").addClass("training_custom_levels").append(
		template_training_custom_spend(custom["points_spend"])
	);
	for(var i=0;i<6;i++)
	{
		$d.append(template_training_custom_team(custom["team"+(i+1)]));
	}
	$d.append(
		template_training_custom_total(),
		$("<div />").addClass("msgbuttons").append(
			make_button(training_content[12],"training_custom_clear()"),
			" ",
			make_button(global_content["reset"],"training_custom_reset()")
		),
		$("<div />").addClass("last_save").html(custom["saved"])
		);
	$(".training_custom_levels").remove();
	$(".custom_training").append($d);
	if(!init && custom["points_spend"]==0) post_training_custom(1);
}
function template_training_custom_spend(points)
{
	var $d = $("<div />").addClass("border_bottom").append(
		$("<div />").addClass("points_label large").html("Points to spend"),
		template_training_custom_bar(points,12).addClass("available_points")
		);
	return $d;
}
function template_training_custom_bar(points,max)
{
	var $d = $("<div />").addClass("level_bar");
	for(var i=0;i<max;i++)
	{
		var $p = $("<div />").addClass("point");
		if(max==1) $p.addClass("single_point");
		else
		{
			if(i==0) $p.addClass("first_point");
			else if(i==max-1) $p.addClass("last_point");
		}
		if(i<points) $p.addClass("point_on");
		$d.append($p);
	}
	return $d;
}
function template_training_custom_team(team)
{
	var skills = [];
	for(var i in team["skills"])
	{
		skills.push(global_content[team["skills"][i]]);
	}

	var $d = $("<div />").addClass("border_bottom team"+team["num"]).append(
		$("<div />").addClass("training_custom_team_label").append($("<span />").addClass("large").html(team["label"]),"<br />",skills.join(", ")),
		$("<div />").addClass("training_custom_team_actions").append(
			$("<div />").addClass("plusminus minus").attr("team","team"+team["num"]+"").click(function(){
				if($(this).hasClass("disabled")) return;
				var t = $(this).attr("team");
				custom_training[t]["points"]--;
				custom_training["points_spend"]++;
				template_training_custom();

			}),
			template_training_custom_bar(team["points"],4),
			$("<div />").addClass("plusminus plus").attr("team","team"+team["num"]+"").click(function(){
				if($(this).hasClass("disabled")) return;
				var t = $(this).attr("team");
				custom_training[t]["points"]++;
				custom_training["points_spend"]--;
				template_training_custom();

				})
		)
	);
	if(team["points"] == 0) $d.find(".plusminus.minus").addClass("disabled");
	if(team["points"] == 4) $d.find(".plusminus.plus").addClass("disabled");
	if(custom_training["points_spend"] == 0) $d.find(".plusminus.plus").addClass("disabled");
	if(custom_training["points_spend"] == 12) $d.find(".plusminus.minus").addClass("disabled");
	return $d;
}
function template_training_custom_total()
{
	var total=0;
	var points = [10,7,6,2];
	for(var i=1;i<7;i++)
	{
		var p = custom_training["team"+i]["points"];
		for(var j=0;j<p;j++) total+=points[j];
	}
	var $d = $("<div />").append(
		$("<div />").addClass("large training_custom_total_label").html(training_content[11]),
		$("<div />").addClass("emboss_outer large training_custom_total").append($("<div />").addClass("embossed").html(total+"/102")).tooltip(pagecontent["p137"])
		);
	return $d;
}
function training_custom_clear()
{
	custom_training["points_spend"] = 12;
	for(var i=1;i<7;i++)
	{
		custom_training["team"+i]["points"]=0;
	}
	template_training_custom();
}
function training_custom_reset()
{
	custom_training["points_spend"] = 0;
	for(var i=1;i<7;i++)
	{
		custom_training["team"+i]["points"]=2;
	}
	template_training_custom();
}
// ** Transfer options ** //
function pop_transfer_options(option,arg1,arg2)
{
	var $msg = $("#msg");
	var $html = $("<div class='std'/>");
	if(option == "transferlist")
	{
		pop_transferlist_player(player_id,0,player_name,0);
		return false;
	}
	else if(option == "set_asking")
	{
			var str = "<h2>"+pagecontent[50]+"</h2>"+
			"<form method='post' action='' id='set_asking_form'><input type='hidden' value='"+player_id+"' name='player_id'>"+
			"<label for='asking_price'>"+pc_replace(pagecontent[51], {"[player]":"<strong class='large'>"+player_name+"</strong>"})+" </label><input type='text' class='emboss' value='"+arg1+"' name='asking_price' id='asking_price' style='width:250px;' tabindex='"+(tabindex++)+"'/><br />"+
			"<input type='checkbox' id='auto_transferlist' value='1' name='auto_transferlist' "+(arg2==1?" checked":"")+" tabindex='"+(tabindex++)+"'/><label for='auto_transferlist'> "+pagecontent[52]+"</label>"+
			"</form>"+
			"<div class='msgbuttons' >"+make_button(global_content["save"],"javascript:post_asking_price()","","",tabindex++)+
			" "+make_button(global_content["cancel"],"javascript:modal.hide();","","",tabindex++)+"</div>";
			$html.html(str);
	}
	else if(option == "reject")
	{
		$html.html("<h2>"+pagecontent[54]+"</h2><p>"+pc_replace(pagecontent[53], {"[player]":"<strong class='large'>"+player_name+"</strong>"})+"</p><form method='post' action='' id='set_asking_form'><input type='hidden' value='"+player_id+"' name='player_id'><input type='hidden' value='0' name='asking_price'></form><div class='msgbuttons'>"+make_button(global_content["save"],"javascript:post_asking_price()","","",tabindex++)+" "+make_button(global_content["cancel"],"javascript:modal.hide();","","",tabindex++)+"</div>");
	}
	else if(option == "approach")
	{
			$html.html("<h2>"+pagecontent[55]+"</h2><form method='post' action='' id='approach_form'><label for='approach_bid'>"+pagecontent[56]+" </label><input type='text' name='approach_bid' id='approach_bid' value='"+arg1+"' class='emboss' /><input type='hidden' value='"+player_id+"' name='player_id'><p>"+pc_replace(pagecontent[57], {"[bid]":"<strong><span id='outbid_val'>"+calculate_outbid(arg1)+"</span></strong>"})+"</p><label for='approach_text'>"+pc_replace(pagecontent[58], {"[clubname]":"<strong>"+arg2+"</strong>"})+"</label><textarea class='emboss' rows='4' cols='70' id='approach_text' name='approach_text'></textarea></form><div class='msgbuttons'>"+make_button(global_content["save"],"javascript:approach_to_buy()","","",tabindex++)+" "+make_button(global_content["cancel"],"javascript:modal.hide();","","",tabindex++)+"</div>");
	}
	else
	{
		return false;
	}
	modal.html($html).show();
	$("#approach_bid").keyup(function(){
		var outbid = calculate_outbid($(this).val());
		$("#outbid_val").text(outbid);
	});
	make_checkbox("#auto_transferlist",true);
	emboss();
}

function post_asking_price()
{
	var asking_price = $("#asking_price").val();
	var auto_accept = $("#auto_transferlist").is(":checked") ? 1 : 0;
	modal.transition(get_loading_img(),function(){
		$.post("/ajax/transfer_bids.ajax.php",{"type":"set_asking","asking_price":asking_price,"auto_accept":auto_accept,"player_id":player_id},function(data){
			if(data != null)
			{
				var html = "<div class='std'>"+"<h2>"+pagecontent[59]+"</h2>";
				if(parseInt(data["asking_price"]) > 0)
				{
					html += "<p>"+pc_replace(pagecontent[60], {"[price]":""+data["asking_price"]+""})+"</p>";
				}
				else
				{
					html += "<p>"+pagecontent[61]+"</p>";
				}
				html += "<div class='msgbuttons'>"+make_button(global_content["ok"],"javascript:page_refresh()","","","",tabindex++)+"</div>"
				+"</div>";
				modal.html(html).banner(); //.fadeIn(200);
				body_refresh();
			}
		},"json");
	});
}
function post_fire_player()
{
	modal.transition(get_loading_img(),function(){
		$.post("/ajax/players_fire.ajax.php",{"type":"confirm","player_id":player_id},function(data){
			if(data != null)
			{
				var $html = $("<div class='std'/>");
				if(data["fire"])
				{
					$html.html("<h2>"+pagecontent[62]+"</h2><p>"+pc_replace(pagecontent[63], {"[player]":"<strong class='large'>"+player_name+"</strong>"})+"</p><div class='msgbuttons'>"+make_button(global_content["ok"],"javascript:page_refresh();","","",tabindex++)+"</div>");
				}
				else
				{
					$html.html("<h2>"+pagecontent[64]+"</h2><p>"+data["error"]+"</p><div class='msgbuttons'>"+make_button(global_content["ok"],"javascript:modal.hide();","","",tabindex++)+"</div>");
				}
			}
			modal.html($html).banner();
			body_refresh();
		},"json");
	});
}
function pop_fire_player(player_id)
{
//	create_message_box();
//	var $msg = $("#msg");
//	var $html = $("<div class='std align_center'/>").html(get_loading_img());
	modal.html(get_loading_img());
	$.post("/ajax/players_fire.ajax.php",{"type":"check","player_id":player_id},function(data){
		if(data != null)
		{
			if(data["fire"])
			{
				var html = "<h2>"+pagecontent[64]+"</h2><p>"+pc_replace(pagecontent[65], {"[player]":"<strong class='large'>"+player_name+"</strong>"})+"</p><div class='msgbuttons'>"+make_button('<img src="http://static.trophymanager.com/pics/icons/recyclebin.gif" title="'+pagecontent[15]+'" style="position:relative;top: 2px;"> '+global_content["fire"],"javascript:post_fire_player()","","",tabindex++)+" "+make_button(global_content["cancel"],"modal.hide()","","",tabindex++)+"</div>";
			}
			else
			{
				var html = "<h2>"+pagecontent[64]+"</h2><p>"+data["error"]+"</p><div class='msgbuttons'>"+make_button(global_content["ok"],"modal.hide()","","",tabindex++)+"</div>";
			}
			modal.html(html).show();
		}

	},"json");
	emboss();
}
function approach_to_buy()
{
	var bid = $("#approach_bid").val();
	var text = $("#approach_text").val();
	modal.transition(get_loading_img(),function(){
		$.post("/ajax/transfer_bids.ajax.php",{"type":"approach","player_id":player_id,"bid":bid,"text":text},function(data){
			if(data != null)
			{
				var html = "<h2>"+pagecontent[55]+"</h2>";
				if(data["error"] != "")
				{
					html +="<p>"+data["error"]+"</p>";
				}
				else
				{
					html +="<p>"+data["succes"]+"</p>";
				}
				html += "<div class='msgbuttons'>"+make_button(global_content["ok"],"javascript:page_refresh()","","","",tabindex++)+"</div>";
				modal.html(html).banner();
				body_refresh();
			}
		},"json");
	});
}
function calculate_outbid(bid) {
	bidd = bid.replace(/,/g, "");
	bidd = bidd.replace(/\./g, "");
	bidd = Math.floor(1.1*bidd);
	bidd = number_format(bidd);
	return bidd;
}

function pop_player_nick(player_id)
{
	modal.html(get_loading_img()).show();
	$.post("/ajax/player_nickname.ajax.php",{"type":"get","player_id":player_id},function(data){
		if(data != null)
		{
			html = "<h2>"+pagecontent[68]+"</h2>"
			+"<p>"+pagecontent[69]+"</p><p>"+pc_replace(pagecontent[70], {"[large]": "<strong class='large'>", "[/large]":"</strong>"})+" <img src='http://static.trophymanager.com/pics/pro_icon.png' class='pro_icon'></p>"
			+"<div class='error' id='nick_error'></div>"
			+"<label for='new_nickname'>"+pagecontent[71]+" </label><input type='text' name='new_nickname' id='new_nickname' class='emboss' maxlength='30' tabindex='"+(tabindex++)+"'/>"
			+"<input type='hidden' name='current_nickname' id='current_nickname' />"
			+"<div class='msgbuttons'>"+make_button(pagecontent[68],"change_player_nick("+player_id+",'set')","","",tabindex++)+" "+make_button(global_content["cancel"],"modal.hide()","","",tabindex++)+"</div>"
			+"<hr />"
			+"<h2>"+pagecontent[72]+"</h2>"
			+"<p>"+pagecontent[73]+"</p>"
			+"<div class='msgbuttons'>"+make_button(pagecontent[72],"change_player_nick("+player_id+",'delete')","","",tabindex++)+" "+make_button(global_content["cancel"],"modal.hide()","","",tabindex++)+"</div>";
			modal.html(html);
			$("#current_nickname").val(data["nickname"]);
			$("#new_nickname").val(data["nickname"]);
			emboss();
		}
	},"json");
}

function change_player_nick(player_id,action)
{
	var nick = $("#new_nickname").val();
	var current_nick = $("#current_nickname").val();
	if(nick == "")
	{
		$("#nick_error").html(pagecontent[74]);
		return false;
	}
	if(nick == current_nick && action!="delete")
	{
		$("#nick_error").html(pagecontent[75]);
		return false;
	}
	modal.transition(get_loading_img(),function(){
		$.post("/ajax/player_nickname.ajax.php",{"type":action,"player_id":player_id,"nickname":nick},function(data){
			if(data!=null)
			{
				body_refresh()
				var html = "<h2>"+pagecontent[76]+"</h2>";
				if(data["success"])
				{
					if(action == "delete")
					{
						html +="<p>"+pagecontent[77]+"</p>";
					}
					else
					{
						html +="<p>"+pagecontent[78]+"</p>";
					}
					modal.html(html);
				}
				else
				{
					html +="<p class='error'>"+pagecontent[79]+"</p>";
				}
				html+="<div class='msgbuttons'>"+make_button(global_content["ok"],"modal.hide()","","",tabindex++)+"</div>";
				modal.html(html).banner();
			}
		},"json");
	});
}

function nt_squad(player_id,action)
{
	var country = player_country;
	modal.transition(get_loading_img(),function(){
		$.post("/ajax/national_squad.ajax.php",{"action":action,"player_id":player_id,"country":country},function(data){
			if(data!=null)
			{
				body_refresh()
				var html = "<h2>"+pagecontent[80]+"</h2>";
				if(data["success"])
				{
					if(action == "remove")
					{
						html +="<p>"+pagecontent[81]+"</p>";
					}
					else
					{
						//'add'
						html +="<p>"+pagecontent[82]+pagecontent[107]+"</p>";
					}
					modal.html(html);
				}
				else
				{
					html +="<p class='error'>"+pagecontent[79]+"</p>";
				}
				html+="<div class='msgbuttons'>"+make_button(global_content["ok"],"modal.hide()","","",tabindex++)+"</div>";
				modal.html(html).banner();
			}
		},"json");
	});
}
function nt_short_list_player(player_id,action)
{
	var country = player_country;
	modal.transition(get_loading_img(),function(){
		$.post("/ajax/national_squad.ajax.php",{"action":action,"player_id":player_id,"country":country},function(data){
			if(data!=null)
			{
				body_refresh()
				var html = "<h2>"+pagecontent[80]+"</h2>";
				if(data["success"])
				{
					if(action == "shortlist_remove")
					{
						html +="<p>"+pagecontent[83]+"</p>";
					}
					else
					{
						html +="<p>"+pagecontent[84]+"</p>";
					}
					modal.html(html);
				}
				else
				{
					html +="<p class='error'>"+pagecontent[79]+"</p>";
				}
				html+="<div class='msgbuttons'>"+make_button(global_content["ok"],"modal.hide()","","",tabindex++)+"</div>";
				modal.html(html).banner();
			}
		},"json");
	});
}
function pop_hidden_skills(player_id,confirm)
{
	if(!confirm)
	{
		modal.html(get_loading_img()).show();
		$.post("/ajax/players_hidden_skills.ajax.php",{"type":"get_hidden","player_id":player_id},function(data){
			var $div = $("<div />").append($("<h2 />").html(pagecontent["p123"]));
			if(data["no_pro"])
			{
				$div.append(
					$("<p />").append(
						pc_replace("This will cost [pro] pro days.",{"[pro]":"<span class='large bold'>"+data["pro"]+"</span>"}),
						'<img src="http://static.trophymanager.com/pics/pro_icon.png" class="pro_icon">'
						),
					pagecontent["p125"]+" <a href='/buy-pro/'>"+global_content["buy_pro"]+"</a>",
					$("<div />").addClass("msgbuttons").append(
						make_button(global_content["ok"],"modal.hide()")
						)
					);
			}
			else
			{
				$div.append(
				$("<p />").append(
					pc_replace(pagecontent["p124"],{"[pro]":"<span class='large bold'>"+data["pro"]+"</span>"}),
					'<img src="http://static.trophymanager.com/pics/pro_icon.png" class="pro_icon">'
					),
				$("<p />").html(pc_replace(pagecontent["p126"],{"[player]":"<span class='large bold'>"+data["name"]+"</span>"})),
				$("<div />").addClass("msgbuttons").append(
					make_button(pagecontent["p123"],"pop_hidden_skills("+player_id+",true)","","hidden_skills_button"),
					make_button(global_content["cancel"],"modal.hide()")
				)
				);
			}
			modal.html($div).show();
		},"json");
	}
	else
	{
		button_disable($("#hidden_skills_button"));
		modal.inner().find(".msgbuttons").prepend(get_loading_img());
		$.post("/ajax/players_hidden_skills.ajax.php",{"type":"add_hidden","player_id":player_id},function(data){
			modal.inner().find(".loading").remove();
			if(data["success"])
			{
				$("#hidden_skill_table th").removeClass("subtle");
				$("#hidden_skill_table td.inj").html(data.skills.injuryproneness).tooltip(data.tooltips.injuryproneness);
				$("#hidden_skill_table td.agg").html(data.skills.aggression).tooltip(data.tooltips.aggression);
				$("#hidden_skill_table td.ada").html(data.skills.adaptability).tooltip(data.tooltips.adaptability);
				$("#hidden_skill_table td.pro").html(data.skills.professionalism).tooltip(data.tooltips.professionalism);
				$(".hidden_skills_text").remove();
				pro_update();
				modal.hide();
			}
			else
			{
				modal.inner().html(global_content["error"]).append(
					$("<div />").addClass("msgbuttons").append(
						make_button(global_content["ok"],"modal.hide()")
					)
				)
			}
		},"json");

	}
}
function pop_change_favpos(player_id) {
	var pc1 = pagecontent[109];
	var pc2 = pagecontent[110];
	var pc3 = pagecontent[111];
	modal.html(get_loading_img()).show();
	$.post("/ajax/players_favpos.ajax.php",{"get":"player","player_id":player_id},function(data){
		if(!data.success) {
			modal.html(data.error).show();
			return;
		}
		var $d = $("<div />").append(
			$("<h2 />").	append(pc1),
			$("<p />").append(pc_replace(pc2,{"[name]":data.player.name})," ",pc_replace(pc3,{"[pro]":data.pro+" <img src='http://static.trophymanager.com/pics/pro_icon.png' />"}))
		);
		modal.html($d).show();
		for(var i=0;i<2;i++) {
			var $dd = $("<div />").appendTo($d);
			var pos = data.positions[i];
			if(!pos) pos = {"pos":"","side":""};
			var $sel = make_pos_select("pos_select"+i,pos.pos);
			$dd.append($sel);
			$sel.selectmenu({
				"maxHeight":"250",
				"width": "200",
				"menuWidth":"181"
			});
			var id = "side_select"+i;
			var $sel = make_side_select(id,pos.side);
			$dd.append($sel);
			$sel.selectmenu({
				"maxHeight":"250",
				"width": "120",
				"menuWidth":"101"
			});
		}
		 check_selects();
		 $d.append(
			$("<div />").addClass("msgbuttons").append(
			$(make_button(global_content.change+" ("+data.pro+" <img src='http://static.trophymanager.com/pics/pro_icon.png' />)","")).click(function(){
				 post_change();
			 }),
				 " ",
				 make_button(global_content.cancel,"modal.hide()")
				 )
			 );
		var post_change = function() {
			modal.inner().find(".msgbuttons").append(get_loading_img()).find(".button").hide();
			var pos0 = $("#pos_select0").val();
			var side0 = $("#side_select0").val();
			var pos1 = $("#pos_select1").val();
			var side1 = $("#side_select1").val();
			$.post("/ajax/players_favpos.ajax.php",{"post":"change_favpos","player_id":player_id,"pos0":pos0,"side0":side0,"pos1":pos1,"side1":side1},function(data){
				modal.inner().find(".loading").remove();
				if(data.success) {
					modal.html(
					$("<div />").append(
						"Success!",
						$("<div />").addClass("msgbuttons").append(
							make_button(global_content.ok,"page_refresh()")
						)
					)
					).show();
					pro_update();
					body_refresh();
				} else {
					modal.html("error: "+data.error).show();
				}
			},"json").error(function(){
				modal.inner().find(".loading").remove();
				modal.inner().find(".msgbuttons").append("error...");
			});
		}
	},"json").error(function(){
		modal.html("error...").show();
	});
	function select_disable(id,disabled) {
		$(id).attr("disabled",disabled).selectmenu();
	}
	var positions = [
		{"pos":"","short":"-", "long":"-"},
		{"pos":"d","short":global_content.defender_abbr, "long":global_content.defender},
		{"pos":"dm","short":global_content.defensive_midfielder_abbr, "long":global_content.defensive_midfielder},
		{"pos":"m","short":global_content.midfielder_abbr, "long":global_content.midfielder},
		{"pos":"om","short":global_content.offensive_midfielder_abbr, "long":global_content.offensive_midfielder},
		{"pos":"f","short":global_content.forward_abbr, "long":global_content.forward}
	];
	var sides = [
		{"side":"","short":"-", "long":"-"},
		{"side":"l","short":global_content.left_abbr,"long":global_content.left},
		{"side":"c","short":global_content.center_abbr,"long":global_content.center},
		{"side":"r","short":global_content.right_abbr,"long":global_content.right},
	];
	function make_pos_select(id,pos) {
		var $s = $("<select />").attr("id",id).change(check_selects);
		for(var i in positions) {
			var p = positions[i];
			var $o = $("<option />").val(p.pos).append(p.long).appendTo($s);
			if(p.pos == pos) $o.attr("selected",true);
		}
		return $s;
	}
	function make_side_select(id,side) {
		var $s = $("<select />").attr("id",id).change(check_selects);
		for(var i in sides) {
			var s = sides[i];
			var $o = $("<option />").val(s.side).append(s.long).appendTo($s);
			if(s.side == side) $o.attr("selected",true);
		}
		return $s;
	}
	var check_selects = function(event) {
		if(event) var change_elem = event.currentTarget;
		$pos0 = $("#pos_select0").attr("disabled",false);
		$side0 = $("#side_select0").attr("disabled",false);
		$pos1 = $("#pos_select1").attr("disabled",false);
		$side1 = $("#side_select1").attr("disabled",false);
		if(event)  {
			if($side0.val() != $side1.val() && $side0.val() && $side1.val() && $(change_elem).attr("id") == $pos0.attr("id")) {
				$pos1.val($pos0.val()).attr("disabled",true);
			} else if($side0.val() != $side1.val() && $side0.val() && $side1.val() && $(change_elem).attr("id") == $pos1.attr("id")) {
				$pos0.val($pos1.val());
				$pos1.attr("disabled",true);
			} else if($pos0.val() != $pos1.val() && $pos0.val() && $pos1.val() && $(change_elem).attr("id") == $side0.attr("id")) {
				$side1.val($side0.val()).attr("disabled",true);
			} else if($pos0.val() != $pos1.val() && $pos0.val() && $pos1.val() && $(change_elem).attr("id") == $side1.attr("id")) {
				$side0.val($side1.val());
				$side1.attr("disabled",true);
			}
		}

		if($pos0.val() == "f") $side0.val("c").attr("disabled",true);
		else if($pos1.val() == "f") $side1.val("c").attr("disabled",true);
		else if($pos0.val() == $pos1.val() && $side0.val() != $side1.val() && !event) $pos1.attr("disabled",true);
		else if($side0.val() == $side1.val() && $pos0.val() != $pos1.val() && !event) $side1.attr("disabled",true);
		$pos0.selectmenu();
		$pos1.selectmenu();
		$side0.selectmenu();
		$side1.selectmenu();
	}
}